package com.example.robotwatcher;

import androidx.appcompat.app.AppCompatActivity;

import android.accessibilityservice.AccessibilityService;
import android.annotation.SuppressLint;
import android.content.ContentValues;
import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;
import android.net.ConnectivityManager;
import android.net.DhcpInfo;
import android.net.NetworkInfo;
import android.net.wifi.WifiManager;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.preference.PreferenceManager;
import android.util.Log;
import android.view.View;
import android.database.sqlite.SQLiteDatabase;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.EditText;
import android.widget.Toast;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.net.InetAddress;
import java.net.Socket;
import java.net.URL;
import java.text.DecimalFormat;
import java.util.Locale;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

public class MainActivity extends AppCompatActivity {


    private EditText ed1;
    private EditText ed2;
    private SharedPreferences preferences;
    private SharedPreferences.Editor editor;
    private CheckBox memorize;
    private String IP;
    private static String line;
    private static int flag;
    private static int  dk;
    private static String ac;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);


        ed1 = (EditText) findViewById(R.id.editText);
        ed2 = (EditText) findViewById(R.id.editText2);
        preferences = PreferenceManager.getDefaultSharedPreferences(this);
        memorize = (CheckBox) findViewById(R.id.checkBox3);

        boolean isRemember = preferences.getBoolean("memorize_IP&PC", false);
        if (isRemember) {

            IP = preferences.getString("defult", "");
            String PC = preferences.getString(IP, "");
            ed1.setText(IP);
            ed2.setText(PC);
            memorize.setChecked(true);


        }
    }

    public static void ping1(String ipAddress) throws Exception {
            line = null;
            flag = 0;
        try {
            Process pro = Runtime.getRuntime().exec("ping " + ipAddress);
            BufferedReader buf = new BufferedReader(new InputStreamReader(pro.getInputStream()));
            dk = buf.read();
            line = String.valueOf(dk);
            if (line!=null)
            {
                flag = 1;
            }

        } catch (Exception ex) {

            ex.printStackTrace();
        }

    }

    public void onClick (View v) throws Exception {

        IP = ed1.getText().toString();
        new Thread(new Runnable() {
            @Override
            public void run() {

                try {
                      ping1(IP);
                      mhandler.sendEmptyMessage(0);
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }
        }).start();

    }

    Handler mhandler = new Handler(){
        public void handleMessage(Message msg){
            String PC = ed2.getText().toString();
            String hp = preferences.getString(IP,"");

            if (flag==1)
            {
                if (hp!=null) {
                    editor = preferences.edit();
                    if (memorize.isChecked()) {
                        editor.putBoolean("memorize_IP&PC", true);
                        editor.putString(IP, PC);
                        editor.putString("defult", IP);

                    } else {
                        editor.clear();

                    }
                    editor.apply();
                    Toast.makeText(MainActivity.this, "启动成功", Toast.LENGTH_SHORT).show();
                    Intent intent = new Intent(MainActivity.this, MainInterface.class);
                    intent.putExtra(IP, PC);
                    startActivity(intent);
                    finish();
                } else
                {

                    editor = preferences.edit();
                    if (memorize.isChecked()) {
                        editor.putBoolean("memorize_IP&PC", true);
                        editor.putString(IP, PC);
                        editor.putString("defult", IP);

                    } else {
                        editor.clear();

                    }
                    editor.apply();
                    Toast.makeText(MainActivity.this, "启动成功", Toast.LENGTH_SHORT).show();
                    Intent intent = new Intent(MainActivity.this, MainInterface.class);
                    intent.putExtra(IP, PC);
                    startActivity(intent);
                    finish();
                }
            }else
            {
                Toast.makeText(MainActivity.this, "启动失败,无法连接目标", Toast.LENGTH_SHORT).show();
            }
        }
    };




}